package com.crm.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class zjname {
	
	 
public static void main(String[] args) {
	try{
		
		  StringBuffer  table = new StringBuffer("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body><table border=1><tr><td>公司名称</td><td>检索内容</td><td></td><td></td></tr>");
          
		 try {
             String encoding="UTF-8";
             File file=new File("d:\\db.txt");
             if(file.isFile() && file.exists()){ //判断文件是否存在
                 InputStreamReader read = new InputStreamReader(
                 new FileInputStream(file),encoding);//考虑到编码格式
                 BufferedReader bufferedReader = new BufferedReader(read);
                 String lineTxt = null;
                 String htmls ="";
                 while((lineTxt = bufferedReader.readLine()) != null){
                	try{
                		 Document doc = Jsoup.connect("http://www.qixin.com/search").data("key",lineTxt).data("page","1").header("Referer", "http://www.qixin.com/").header("Host", "www.qixin.com").header("Cookie", "aliyungf_tc=AQAAAHMziEC7vgcAergFJKuHYUzttFKO; channel=baidu; cookieShowLoginTip=1; sid=s%3AP5iHmfh_09kJDwpI9e9zZGerDFcn-_x-.28pr1yJsb1tlK1U3dKK3P17zNg8odY2JZbDMCc8uDSs; responseTimeline=24; _zg=%7B%22uuid%22%3A%20%2215d7e4ffcb06e-08f52b5cf-424e002e-1fa400-15d7e4ffcb1626%22%2C%22sid%22%3A%201501062757.554%2C%22updated%22%3A%201501065952.289%2C%22info%22%3A%201501062757558%2C%22cuid%22%3A%20%227106b52a-c1a1-443a-8a47-7a93d8e2644b%22%7D; Hm_lvt_52d64b8d3f6d42a2e416d59635df3f71=1501062757; Hm_lpvt_52d64b8d3f6d42a2e416d59635df3f71=1501065952").header("Connection", "keep-alive").timeout(62000).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36").ignoreContentType(true).timeout(62000).get();
                		 Elements  els = doc.select(".company-item");
                		 if(els!=null&&els.size()>0){
                			 Element  el = els.get(0);
                			  htmls +="<tr><td>"+lineTxt+"</td>";
                			 Element eltwo = el.select(".col-2-1").get(0);
                			String title = "";// eltwo.select(".company-title").text();
                			Elements  elthree =  eltwo.select(".legal-person"); 
                			if(elthree!=null&&elthree.size()>0){
                				for(Element e:elthree){
                					title +="<td>"+e.text()+"</td>"   ;
                				} 
                			}
                			htmls +=title+"<tr>";
                			// String  s = el.select(".col-2-1").html();
                			 System.out.println(title);
                		 }
                		 try {
 		                     //sleep()方法导致了程序暂停执行指定的时间，让出cpu该其他线程，
 		                 //但是他的监控状态依然保持者，当指定的时间到了又会自动恢复运行状态。
 		                     //在调用sleep()方法的过程中，线程不会释放对象锁。
		     		                   Thread.sleep(3000);
		     		       } catch (Exception e) {
		     	                    e.printStackTrace();
		     		  }
                	}catch(Exception e){
                		e.printStackTrace();
                	}
                	
            		  
                    // System.out.println();
                 }
                 table.append(htmls+"</table></body></html>");
                 PrintStream printStream = new PrintStream(new FileOutputStream("d://reportss.html")); 
                 printStream.println(table.toString()); 
                 System.out.println("已成功输出");
                 
                 read.close();
		     }else{
		         System.out.println("找不到指定的文件");
		     }
		     } catch (Exception e) {
		         System.out.println("读取文件内容出错");
		         e.printStackTrace();
		     }
				

		
		
		// System.out.println(doc);
		// String address = doc.select(".datas_table tbody tr").get(3).select("td").get(0).text();
		
	}catch(Exception e){
		e.printStackTrace();
	}
	
}
}
